<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
		
	abstract class My_Controller extends CI_Controller
	{
		public $m_arrSub = array();
		
		public $data     = array();
		
		public  $arrTpl = array();
		
		public $m_arrUser = array();
		
		
		function __construct()
		{
			parent::__construct();
			
			$this->m_arrUser = $this->my_member->getUser();
		}
		
		//***************   组合控制器  Start     **************//
		public function getSub(){
			foreach ($this->m_arrSub as $class => $method)
			{
		
				if (file_exists(APPPATH.'controllers/'.$class.'.php'))
				{
					include_once(APPPATH.'controllers/'.$class.'.php');
		
					$class = substr($class, strpos($class, '/')+1);
						
					$subCI = new $class();
		
					$this->data[$class] = call_user_func_array(array(&$subCI, $method), array_slice($this->uri->rsegments, 2));
				}
				
			}
		}
		
		//***************   组合控制器  End **************//
		
		
		//************************ 设置输出数据 Start**********************************//
		
		public function setTitle($title) 
		{
			$GLOBALS['_doc']['title'] = $title;
		}
		
		public function getTitle() 
		{
			return $GLOBALS['_doc']['title'];
		}
		
		public function setDescription($description) 
		{
			$GLOBALS['_doc']['description'] = $description;
		}
		
		public function getDescription() 
		{
			return $GLOBALS['_doc']['description'];
		}
		
		public function setKeywords($keywords) 
		{
			$GLOBALS['_doc']['keywords'][md5($keywords)] = $keywords;
		}
		
		public function getKeywords() 
		{
			return $GLOBALS['_doc']['keywords'];
		}
		
		public function addLink($href, $rel,$sizes = "72x72") 
		{
			$GLOBALS['_doc']['links'][md5($href)] = array(
					'href' => $href,
					'rel'  => $rel,
					'size'=> $sizes,
			);
		}
		
		public function getLinks() 
		{
			return $GLOBALS['_doc']['links'];
		}
		
		public function addStyle($href, $rel = 'stylesheet', $media = 'screen') 
		{
			$GLOBALS['_doc']['styles'][md5($href)] = array(
					'href'  => $href,
					'rel'   => $rel,
					'media' => $media
			);
		}
		
		public function getStyles() 
		{
			return $GLOBALS['_doc']['styles'];
		}
		


		public function addScript($script) 
		{
			$GLOBALS['_doc']['scripts'][md5($script)] = $script;
		}
		
		public function getScripts() 
		{
			return $GLOBALS['_doc']['scripts'];
		}
		
		
		public function addSubmenu($name, $href, $attribute = '') 
		{
			$GLOBALS['_doc']['submenu'][md5($name)] = array(
					'name'		=> $name,
					'href'		=> $href,
					'attribute'	=> $attribute
			);
		}
		
		public function getSubmenu() 
		{
			return $GLOBALS['_doc']['submenu'];
		}
		
		public function setCurPage($page) 
		{
			$GLOBALS['_doc']['current_page'] = $page;
		}
		
		public function getCurPage() 
		{
			return $GLOBALS['_doc']['current_page'];
		}
		
		//************************ 设置输出数据 End ******************************//
		
		
		
		//************************ oauth2 更新集合函数  Start******************************//
		
		public function oauth2($acc2platList)
		{
			// 已经绑定了
			if (!empty($acc2platList))
			{
			
				$accInfo  = $this->account_m->getAccount(array('id' => $acc2platList[0]['accountID']));
			
			
				$token    = createToken();
				$lastIP   = $this->input->server('REMOTE_ADDR'); // ip
				$lastTime = time();
				$toSet = array(
						'token'    => $token,
						'lastIP'   => $lastIP,
						'lastTime' => $lastTime,
				);
			
			
				$this->account_m->setAccount($accInfo['id'],$toSet);
			
			
				$this->setPlatform($acc2platList[0]['accountID']);
				
				$accInfo['token']    = $token;
				$accInfo['lastIP']   = $lastIP;
				$accInfo['lastTime'] = $lastTime;
			
				$this->my_member->login($accInfo,$this->m_outTime);
			
				$this->load->view('account/oauth2');
			
			}
			else  // 还未注册或者未绑定
			{
				redirect('account/reg');
			}
		}
		
		public function setPlatform($accountID)
		{
			$this->load->model('account/account_m');
			$this->arrTpl = $this->config->item('my_session_key');
			foreach($this->arrTpl as $sessionKey => $type)
			{
				if ($this->session->userdata($sessionKey) != false)
				{
					$filter = array('accountID' => $accountID,'platformID' => $type);
					$platform = $this->account_m->getPlatfromList($filter);
					$sessionData =  $this->session->userdata($sessionKey);
					if (!empty($platform) && is_array($platform))
					{
						$updateData = array(
								'openID'  => $sessionData['openID'],
								'token'   => $sessionData['token'],
								'retoken' => $sessionData['retoken'],
								'outTime' => $sessionData['outTime'],
									
						);
						$this->account_m->setPlatform($accountID,$type,$updateData);
		
					}
					else
					{
						$addData = array(
								'accountID' => $accountID,
								'platformID'=> $type,
								'addTime'   => date('Y-m-d H:i:s',time()),
								'openID'  	=>   $sessionData['openID'],
								'token'   	=>  $sessionData['token'],
								'retoken' 	=>    $sessionData['retoken'],
								'outTime' 	=> $sessionData['outTime'],
									
						);
		
						$this->account_m->addPlatform($addData);
					}
						
						
					$this->session->unset_userdata($sessionKey);
				}
			}
		}
		
		
		//************************ oauth2 更新集合函数 End ******************************//

	}
	
	//前台控制器扩展例子
	abstract class Front_Controller extends My_Controller
	{
		

	}